package com.vueperson.vueperson.t1.CreationalPattern.BuildingPattern.DirectorTest;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class XMLUtil {

    //该方法用于XML配置文件中提取图表类型,并返回类型名
    public static Object getBean(){
        File directory = new File("");//设定为当前文件夹


        try {
            //创建文档对象
            DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=documentBuilderFactory.newDocumentBuilder();
            Document document;
//            D:\Users\vue_project\vueperson\src\test\java\com\vueperson\vueperson\t1\CreationalPattern\BuildingPattern\BuildActor\XMLUtil.java
            document=builder.parse(new File("src\\test\\java\\com\\vueperson\\vueperson\\t1\\CreationalPattern\\BuildingPattern\\DirectorTest\\config.xml"));

            //获取包含class的文本结点
            NodeList nl=document.getElementsByTagName("className");
            Node classNode=nl.item(0).getFirstChild();
            String className=classNode.getNodeValue().trim();

            //通过类名生成实例对象
            Class c=Class.forName(className);
            Object obj=c.getDeclaredConstructor().newInstance();
            return obj;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
